package com.gzxw.mogublog.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gzxw.mogublog.base.global.BaseSysConf;
import com.gzxw.mogublog.commons.entity.Blog;
import com.gzxw.mogublog.commons.entity.Tag;
import com.gzxw.mogublog.commons.entity.WebConfig;
import com.gzxw.mogublog.commons.entity.WebNavbar;
import com.gzxw.mogublog.utils.R;
import com.gzxw.mogublog.xo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private WebNavbarService webNavbarService;

    @Autowired
    private WebConfigService webConfigService;

    @Autowired
    private BlogService blogService;

    @Autowired
    private TagService tagService;

    @Autowired
    private SysParamsService sysParamsService;


    // 获取导航栏列表
    @GetMapping("/getWebNarbar")
    public R getWebNarbar() {

        List<WebNavbar> list = webNavbarService.getAllList();

        return R.success("200", list);
    }


    @GetMapping("/getWebConfig")
    public R getWebConfig() {

        WebConfig webConfig = webConfigService.getWebConfigByShowList();

        return R.success("200", webConfig);
    }

    @GetMapping("/getBlogByLevel")
    public R getBlogByLevel(@RequestParam("level") Integer level,
                            @RequestParam(value = "currentPage",required = false) Long currentPage,
                            @RequestParam("useSort") Integer useSort) {

        IPage<Blog> pageList = blogService.getBlogPageByLevel(level, currentPage, useSort);

        Map<String, Object> result = new HashMap<>();
        List<Blog> records = pageList.getRecords();
        long total = pageList.getTotal();
        long size = pageList.getSize();
        long current = pageList.getCurrent();
        result.put("records", records);
        result.put("total", total);
        result.put("size", size);
        result.put("current", current);
        return R.success("200", result);
    }

    @GetMapping("/getNewBlog")
    public R getNewBlog(@RequestParam("currentPage") Long currentPage,
                        @RequestParam("pageSize") Long pageSize) {

        IPage<Blog> pageList = blogService.getNewBlog(currentPage, pageSize);

        Map<String, Object> result = new HashMap<>();
        List<Blog> records = pageList.getRecords();
        long total = pageList.getTotal();
        long size = pageList.getSize();
        long current = pageList.getCurrent();
        result.put("records", records);
        result.put("total", total);
        result.put("size", size);
        result.put("current", current);

        return R.success("200", result);
    }


    @GetMapping("/getHotTag")
    public R getHotTag() {

        String hotTagCount = sysParamsService.getSysParamsValueByKey(BaseSysConf.HOT_TAG_COUNT);

        List<Tag> tagList = tagService.getHotTag(Integer.valueOf(hotTagCount));

        return R.success("200",tagList);
    }


}
